package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * <新闻 - 东莞阳光新闻网> 解析类
 *
 * @author KK
 *
 */
public class Sun0769NewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex={
			"http://[a-z]*.sun0769.com/[a-z]+/[0-9]+/t[0-9]+_[0-9]+.shtml",
			"http://[a-z]*.sun0769.com/[a-z]+/[a-z]+/[0-9]+/t[0-9]+_[0-9]+.shtml",
			"http://[a-z]*.sun0769.com/[a-z]+/[a-z]+/[a-z]+/[0-9]+/t[0-9]+_[0-9]+.shtml",
			"http://[a-z]*.sun0769.com/html/question/[0-9]+/[0-9]+.shtml",
		};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String htmltxt=urlMeta.getHtml();
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String author =null;
		
		title = doc.select("div.article_hd h1").text();
		if(StringUtils.isBlank(title)){
			doc.select("div.title span.pagenum").remove();
			title = doc.select("div.title").text();
		}
		if(StringUtils.isBlank(title)){
			Elements titleList = doc.select("div.cleft strong.tgray14");
			if(titleList.size() > 0){
				title = titleList.get(0).text();
			}
		}
		if(StringUtils.isBlank(title)){
			title = doc.select("h1#docTitleBox").text();
		}
		
		date = DateUtils.matchDate(doc.select("div.comment").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("div.titleMain").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("p.te12h").text());
		if(date==null)
			date = DateUtils.matchDate(doc.select("div#time_comment_box").text());
		
		content = HtmlCleaner.getContentHtml(url, doc.select("div.TRS_Editor"));
		if(StringUtils.isBlank(content)){
			doc.select("div.audit").remove();
			doc.select("div.shengming").remove();
			content = HtmlCleaner.getContentHtml(url, doc.select("div.text14_2"));
		}
		
		author = JSoupUtils.matchAuthor(doc, "来源：");
		
		NewsMeta news=new NewsMeta();
		news.setTitle(title);
		news.setDate(date);
		news.setContent(content);
		news.setUrl(url);
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		Sun0769NewsAnalyse a=new Sun0769NewsAnalyse();
		String url="http://tour.sun0769.com/world/got/info/201305/t20130530_2096750.shtml";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
